﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// ERP_出库结果回写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ERP_ShipResultController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string gz_con;


        public ERP_ShipResultController(ILogger<ERP_ShipResultRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            gz_con = _configuration.GetConnectionString("GZ_WMS_DbConnection");
            _logger = loggerFactory.CreateLogger<ERP_ShipResultController>();

        }

        /// <summary>
        /// ERP_出库结果回写
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostERP_ShipResult([FromBody] ERP_ShipResultRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            ERP_ShipResultResponseDto re = new ERP_ShipResultResponseDto();

            if (strparm.DATA==null)
            {
                strMsg = "DATA不允许为空！";
            }

            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
            try
            {
                if (strMsg == "")
                {
                    var row = strparm.DATA;
                    foreach (var item in row)
                    {
                        string[] LinesArray = null;
                        LinesArray = item.ORDER_LINE.Split('.');
                        string order_line = LinesArray[0].ToString().Trim();
                        dic = new Dictionary<string, string>();
                        dic.Add("ORDERNO", item.SOURCENUM);
                        dic.Add("REFLINENO", order_line);
                        string  sqlstr= MethodHelp.GetTranSQL("TMS0102", dic, con);//查询当前客检记录是否存在,MYSQL获取语句
                        DataTable check_ccode = DB.OracleQuery(sqlstr.ToString(),gz_con);//Oracle驱动查询
                        if (check_ccode.Rows.Count <= 0)
                        {
                            strMsg = strMsg + "ORDER_NUMBER=<" + item.SOURCENUM + ">,LINE=<"+order_line + "> 找不到出库数据，请联系管理员进行排查。";
                        }                        
                    }

                    if (strMsg=="")
                    {
                        foreach (var item2 in row)
                        {
                            //行号获取大行号
                            string[] LinesArray = null;
                            LinesArray = item2.ORDER_LINE.Split('.');
                            string order_line = LinesArray[0].ToString().Trim();

                            //判断返回是否成功
                            string PROCESSSTATUS = "";
                            if (item2.PROCESSSTATUS== "COMPLETED")
                            {
                                PROCESSSTATUS = "Y";
                            }
                            else
                            {
                                PROCESSSTATUS = "W";
                            }


                            dic = new Dictionary<string, string>();
                            dic.Add("ORDERNO", item2.SOURCENUM);
                            dic.Add("REFLINENO", order_line);
                            dic.Add("EDISENDFLAG", PROCESSSTATUS);
                            dic.Add("EDIERRORMESSAGE", item2.PROCESSMESSAGE);
                            string sqlstr = MethodHelp.GetTranSQL("TMS0103", dic, con);//查询当前客检记录是否存在,MYSQL获取语句
                            if (sqlstr!="")
                            {
                                strSqlList.Add(sqlstr.ToString()) ;
                            }

                        }

                        if (strSqlList.Count > 0)
                        {
                            bool A = DB.OracleExecuteNoQueryTran(strSqlList, gz_con);
                            if (A)
                            {

                            }
                            else
                            {
                                strMsg = "Database update failed";
                            }

                        }
                        else
                        {
                            strMsg = "No insertion data present";
                        }
                    }
                  
                }               
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }
            if (strMsg=="")
            {
                re.code = 0;
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }

            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);
        }

      
    }
}
